package DP;

import org.junit.Test;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: DP
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-22  11:20
 * @Description: TODO
 * @Version: 1.0
 */
public class 斐波那契数列501 {
    // 使用了一位数组的方式
    public int fib(int n) {
        if (n <= 1) {
            return n;
        }
        int[] result = new int[n + 1];
        result[0] = 0;
        result[1] = 1;
        for (int i = 2; i <= n; i++) {
            result[i] = result[i - 1] + result[i - 2];
        }
        return result[n];
    }

    // 使用变量
    public int fib2(int n) {
        if (n <= 1) {
            return n;
        }
        int a = 0;
        int b = 1;
        int c = 0;
        for (int i = 1; i < n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }

    @Test
    public void test(){
        int i = fib2(6);
        System.out.println(i);
    }

}
